#对文件进行操作

import tkinter
import os


def comm_file(path):
    path = path.replace('#', ' ')
    root = tkinter.Tk()
    root.title('操作')
    # 设置窗口大小
    width = 400
    height = 100
    # 获取屏幕尺寸以计算布局参数，使窗口居屏幕中央
    screenwidth = root.winfo_screenwidth()
    screenheight = root.winfo_screenheight()
    alignstr = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
    root.geometry(alignstr)
    en = tkinter.Entry(root)
    en.pack(fill=tkinter.X)
    myFrame = tkinter.Frame(root)
    myFrame.pack(fill=tkinter.X)
    btnadd = tkinter.Button(myFrame,text='添加', command=lambda: addFile(en.get()))
    btndel = tkinter.Button(myFrame,text='删除',command = lambda :delFile(en.get()))
    btnchange = tkinter.Button(myFrame,text='修改名字', command = lambda: changeFileName(path,en.get()))

    btnadd.pack(side=tkinter.LEFT)
    btndel.pack(side=tkinter.LEFT)
    btnchange.pack(side=tkinter.LEFT)

    en.insert(0,path)

    root.mainloop()

def addFile(path):
    if not os.path.exists(path):
        if path.find('.') == -1:
            os.makedirs(path)
        else:
            fp = open(path, 'w')
            fp.close()
        print(path + ' 创建成功')

def changeFileName(oldpath, path):
    os.rename(oldpath,path)
    print(path + ' 修改成功')

def delFile(path):
    try:
        if os.path.isdir(path):
            os.removedirs(path)  # 删除空文件夹
        else:
            os.remove(path)
        print(path + ' 删除成功')
    except Exception:
        print('文件不存在/文件夹不为空')